package com.wulis.provider.authority.web;

import static com.wulis.common.model.enums.VersionEnum.WEB_V_1_0;

import java.util.List;

import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.wulis.common.annotation.ApiVersion;
import com.wulis.common.model.dto.IdDto;
import com.wulis.common.support.BaseController;
import com.wulis.common.wrapper.ApiWrapMapper;
import com.wulis.common.wrapper.ApiWrapper;
import com.wulis.provider.authority.model.dto.InsertMenuDto;
import com.wulis.provider.authority.model.dto.QueryMenuTreeDto;
import com.wulis.provider.authority.model.dto.UpdateMenuDto;
import com.wulis.provider.authority.model.vo.InsertMenuVo;
import com.wulis.provider.authority.model.vo.QueryMenuTreeVo;
import com.wulis.provider.authority.model.vo.QueryMenuVo;
import com.wulis.provider.authority.service.MenuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author WuliBao
 * @date 2020-04-17
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("web/permission/menu")
@Api(tags = "系统权限管理:菜单管理")
public class MenuController extends BaseController {
    
    private final MenuService menuService;
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryMenuTree")
    @ApiOperation("查询菜单列表(树形)")
    @PreAuthorize("@api.hasPermission('web:permission:menu:queryMenuTree')")
    public ApiWrapper<List<QueryMenuTreeVo>> queryMenuTree(@Valid QueryMenuTreeDto queryMenuTreeDto) {
        return ApiWrapMapper.success(menuService.queryMenuTree(queryMenuTreeDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PostMapping("insertMenu")
    @ApiOperation("新增菜单")
    @PreAuthorize("@api.hasPermission('web:permission:menu:insertMenu')")
    public ApiWrapper<InsertMenuVo> insertMenu(@Valid @RequestBody InsertMenuDto insertMenuDto) {
        return ApiWrapMapper.success(menuService.insertMenu(insertMenuDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PostMapping("queryMenu")
    @ApiOperation("查询菜单")
    @PreAuthorize("@api.hasPermission('web:permission:menu:queryMenu')")
    public ApiWrapper<QueryMenuVo> queryMenu(@Valid @RequestBody IdDto idDto) {
        return ApiWrapMapper.success(menuService.queryMenu(idDto.getId()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PutMapping("updateMenu")
    @ApiOperation("修改菜单")
    @PreAuthorize("@api.hasPermission('web:permission:menu:updateMenu')")
    public ApiWrapper<Integer> updateMenu(@Valid @RequestBody UpdateMenuDto updateMenuDto) {
        return ApiWrapMapper.success(menuService.updateMenu(updateMenuDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @DeleteMapping("deleteMenu")
    @ApiOperation("删除菜单")
    @PreAuthorize("@api.hasPermission('web:permission:menu:deleteMenu')")
    public ApiWrapper<Integer> deleteMenu(@Valid @RequestBody IdDto idDto) {
        return ApiWrapMapper.success(menuService.deleteMenu(idDto.getId()));
    }
}
